home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / basic / tutorqb.com / TUTOR-10.BAS < prev    next >
Encoding:
BASIC Source File  |  1986-10-07  |  13.5 KB  |  271 lines

  1. goto example1
  2. '                       The QuickBASIC Tutorial Series
  3.  
  4. '                                 Lesson 10
  5. '                         QB2 Programming Techniques
  6.  
  7. '                       Copyright 1986  Ford Software
  8.  
  9. '                            4845 Willowbend Blvd
  10. '                              Houston, TX 77035
  11. '                               (713) 721-5205
  12. '                          CompuServe ppn: 71355,470
  13.  
  14. 'This Tutorial may not be copied or distributed in any form - printed, on disk
  15. 'or electronically - without the express written permission of Ford Software.
  16. '          Unlicensed distribution is a violation of copyright law
  17. '           and may be subject to civil and criminal prosecution.
  18.  
  19. 'QuickBASIC is the registered trademark of the Microsoft Corporation.
  20. 'Ford Software is not associated in any way with the Microsoft Corporation.
  21.  
  22. '(Make sure NumLock is off and press PgDn)
  23. 'page 2
  24. '                           I N T R O D U C T I O N
  25.  
  26. 'In this tutorial, we assume that you have some familiarity with BASIC and
  27. 'have used a BASIC interpreter before. If you have not, you should run the
  28. 'earlier lessons in this series before running this one. At the very least,
  29. 'you should first run TUTOR-01 which covers the functioning of the QB2 editor.
  30.  
  31. 'The purpose of this lesson is to go over the features of QB2 that are
  32. 'different from the Microsoft (or IBM) BASIC or BASICA interpreter.
  33.  
  34. 'To run examples in this lesson, first press Ctrl-PgUp to get to the first
  35. 'line of this file. Change the line to "goto example#" where "#" is the number
  36. 'of the example you wish to run. Then return to the page you were on and press
  37. 'Ctrl-R to compile and run the code.
  38.  
  39. 'This tutorial is meant to be used in conjunction with the QB2 manaul. As each
  40. 'topic is introduced in this tutorial, look up that topic in the manual and
  41. 'read about it first. The purpose of this tutorial is to provide interactive
  42. 'reinforcement of the material in the manual and present in a logical order.
  43.  
  44. '(Press PgDn)
  45. 'page 3
  46. '                                 Contents
  47. '
  48. '                 page
  49. '                  4    Labels and Line Numbers
  50. '                  5    Long Lines
  51. '                  6    Code Readability
  52. '                  7    Program Structure
  53. '                  8    Getting Rid of GOTO's
  54. '                  8    ..Interpretive BASIC example
  55. '                  8    ..In QB2 format
  56. '                 10    Replacing IF-THEN With WHILE-WEND
  57. '                 11    Block IF-THEN-ELSEIF-ELSE-END_IF
  58. '
  59. '
  60. '
  61. '  To move directly to a particular page, press Ctrl-F and enter "page n"
  62. '   where "n" is the page number to go to. Then use Ctrl-Down to realign
  63. '   the page on the screen. To search for a topic, press Ctrl-F
  64. '
  65. '
  66. '(Press PgDn)
  67. 'page 4                     Labels and Line Numbers
  68.  
  69. 'QB2 does not require lines to be numbered as the BASIC interpreter does.
  70. 'Sometimes, however, you will need to be able to identify a line or section of
  71. 'code. You can use a line number just like when using the interpreter, or you
  72. 'can use a "label".
  73.  
  74. 'A label can be up to 40 characters of alphanumeric characters. It must be the
  75. 'first non-blank character on the line and must be followed by a colon (:).
  76. 'You do NOT add the colon when using GOTO, GOSUB, etc., just when defining it.
  77. 'In a label, there is no difference between capital and non-capital letters.
  78. 'SORT: is the same as Sort:. Spaces are not allowed in a label. "SORT.DATA:"
  79. 'is a valid label. "SORT DATA:" is not.
  80.  
  81. example1:
  82. TEXT$="Hello"                 'This routine will call the subroutine by the
  83. GOSUB Print.text              'name of "Print.text" to print TEXT$.
  84. END
  85. Print.text:                   'Press Ctrl-R to compile and run this routine.
  86. PRINT TEXT$
  87. RETURN
  88. '      (PgDn)
  89. 'page 5                           Long Lines
  90.  
  91. 'The BASIC interpreter limits lines to 255 characters. Some commands in BASIC,
  92. 'such as FIELD statements or IF-THEN-ELSE constructs, could easily take more
  93. 'than 255 characters. Under the interpreter, you have to find some way to
  94. 'break these into smaller lines.
  95.  
  96. 'QB2 also limits lines in the editor ("physical lines") to 255 characters:     80        90       100       110       120       130       140       150       160       170       180       190       200       210       220       230       240       250
  97. '23456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
  98. 'QB2 allows you to combine several physical lines into one long program line
  99. 'of up to 32,767 characters (a "logical line"). Ending a physical line with
  100. 'an underscore character ("_") indicates that the logical line is continuing.
  101.  
  102. 'Why should you want such a long line? In the interpreter, a common practice
  103. 'is to test for a value and use GOTO to branch around code if the test fails:
  104. '1000 IF X <> 1 THEN GOTO 1050 where the lines between 1000 and 1050 do a lot
  105. 'of stuff that you don't want done unless X=1. In QB2, you would say:
  106. '  IF X=1 THEN_
  107. '    A=B:_            'Usage of GOTO can make code very difficult to follow.
  108. '    C=D:_            'QB2's formatting capabilities make it a lot easier.
  109. '    E=F:_
  110. '    (etc)                                                   (Press PgDn)
  111. 'page 6                        Code Readability
  112.  
  113. 'In QB2, you can still enter code just like you did in the interpreter:
  114.  
  115. '  INPUT "Enter 1-10"; X:FOR I=1 TO 10:IF I=X THEN PRINT I*5 ELSE PRINT I*2
  116. '  IF I=7 THEN PRINT X;:PRINT "I EQUALS 7"
  117. '  NEXT:END
  118.  
  119. 'which is difficult to read, debug and modify. Or you can enter:
  120.  
  121. example2:
  122. INPUT "Enter 1-10"; X       ' Notice the use of "_" to continue a logical line
  123. FOR I=1 TO 10               ' onto the next line. A common mistake is to leave
  124.   IF I=X_                   ' off a "_" or to put one where you didn't really
  125.     THEN PRINT "I*5" I*5_   ' want it. Run this example to see that it works.
  126.     ELSE PRINT "I*2" I*2    ' Remove one of the "_" continuation lines and run
  127.   IF I=7 THEN_              ' it, but before you do, notice what page number
  128.     PRINT X;:_              ' this is so that you can find your way back here.
  129.     PRINT "I EQUALS 7"      ' Notice that you must also have a ":" before the
  130. NEXT                        ' "_" to separate statements on the same logical
  131. END                         ' line. Please study these two examples carefully.
  132. '      (PgDn)
  133. 'page 7                 (Code Readability, continued)
  134.  
  135. 'There are no hard and fast rules for making code more readable. I prefer to
  136. 'spread code out as much as possible; but there is also something to be said
  137. 'for combining small pieces of code onto one line so that you can see more of
  138. 'the code at once. For example, putting "X=1:Y=2:Z=3" onto one line does not
  139. 'impair readability that much and lets you see a couple of more lines on one
  140. 'screen.
  141.  
  142. 'PROGRAM STRUCTURE:
  143. 'A program should be laid out much like a book. It should start with the title
  144. 'and author's name and copyright notice. There should be a version number and
  145. 'a few lines (Introduction) explaining the purpose of the program and listing
  146. 'any associated files. Then you should list the label of each major part of
  147. 'the program with a short description, similar to a Table of Contents.
  148.  
  149. 'The first lines of actual code should be DIM and DEF statements, followed by
  150. 'GOSUBs to different routines such as to intialize program variables, open
  151. 'files, draw the screen, etc. Then go into your main program loop, which calls
  152. 'the subroutines. Load QPRINT to see an example of QB2 program structure.
  153.  
  154. '      (PgDn)
  155. 'page 8                     Getting Rid of GOTO's
  156.  
  157. 'One of the major changes in going from interpretive BASIC to QB2 is getting
  158. 'rid of the GOTO's. Here is a typically snarled piece of code in BASIC:
  159. example3:
  160. 1000 INPUT "ENTER A SINGLE-DIGIT NUMBER"; X
  161. 1010 IF X=1 THEN GOTO 1050
  162. 1020 FOR I=1 TO 10: IF X=I THEN PRINT 1 X
  163. 1030 NEXT
  164. 1040 GOTO 1060
  165. 1050 PRINT 1 "X=1"
  166. 1060 ' lines 1010-1050 in QB2 format:
  167.  IF X=1_
  168.    THEN PRINT 2 "X=1"_
  169.    ELSE FOR I=1 TO 10:_
  170.       IF X=I THEN PRINT 2 X:_  'and here is where things break down.
  171.     NEXT  'Although NEXT is on a seperate line, the "_" in the line above
  172.           'makes these two lines equivalent to
  173. '1020 FOR I=1 TO 10: IF X=I THEN PRINT X: NEXT  'Most people will recognize
  174. 'in this example that the NEXT will not be executed except when X=I, but the
  175. 'QB structure may actually make this more difficult to recognize because it
  176. 'is easy to overlook the "_" at the end of the previous line.      (PgDn)
  177. 'page 9               (Getting Rid of GOTO's, continued)
  178.  
  179. 'So how do we get around this? One way would be to branch around the rest of
  180. IF X=1 THEN_             ' the code if x=1, just as we did in the original
  181.   PRINT 3 "X=1":_        ' code with line numbers. (But we are getting back to
  182.   GOTO Branch.around     ' the old "spagetti code" again when we do that.)
  183. FOR I=1 TO 10            ' Note that the lines on the left no longer have a _
  184.   IF X=I THEN PRINT 3 X  ' at the end of each one. That means that the NEXT no
  185. NEXT                     ' longer is on the same logical line as the IF X...
  186. Branch.around:           ' Run "example3" (which starts on the prior page).
  187. END                      ' Enter "1" when prompted and you will see that all
  188.              ' three sections of code work.
  189.  
  190. 'Run it again and enter "3" at the prompt and notice that the second section
  191. 'of code does not work.
  192.  
  193. 'The reason that the code above works when the code on the previous screen did
  194. 'not is because the IF line in the code above does not end with a "_". These
  195. 'marks are very easy to overlook and can cause some strange error messages.
  196.  
  197.  
  198. '      (PgDn)
  199. 'page 10              (Getting Rid of GOTO's, continued)
  200.  
  201. 'To avoid having to branch around, you can use a WHILE-WEND test instead of a
  202. 'IF-THEN test. Here is the same code with a WHILE-WEND test:
  203. example4:   INPUT "ENTER A SINGLE-DIGIT NUMBER"; X
  204. IF X=1_
  205.   THEN PRINT 4 "X=1"_         'Press Ctrl-PgUp, change the first line to
  206.   ELSE FOR I=1 TO 10:_        '"goto example4", and press Ctrl-R.
  207.      WHILE I=X:_          'Try entering 1 and 2.
  208.        PRINT 4 X:_
  209.        I=I+1:_        ' <- This line is added to keep the WHILE-WEND
  210.      WEND:_               'loop from being endless. Incrementing "I" to
  211.        NEXT                   'break out is a convenient method in this case.
  212. END                           'You usually have to come up with something
  213. 'different each time. Sometimes, you have to create a new variable just for
  214. 'breaking out of a loop if you don't want to change the other variables:
  215. '  LOOP=1:_
  216. '  WHILE (I=X) AND (LOOP=1):_
  217. '    PRINT X:_
  218. '    LOOP=0:_                 'This example would have worked just as well
  219. '  WEND:_                     'in the code above as incrementing "I" did.
  220. '                (PgDn)
  221. 'page 11              (Getting Rid of GOTO's, continued)
  222.  
  223. 'Yet another way to take conditional action in the middle of a long logical
  224. 'line is to GOSUB to a routine that will make the test and take the action if
  225. 'necessary. Using our example:
  226. example5:  INPUT "ENTER A SINGLE-DIGIT NUMBER"; X
  227. IF X=1_                    ' Run this example to see that it works.
  228.   THEN PRINT 5 "X=1":_     ' While this makes the main routine cleaner, it
  229.   ELSE FOR I=1 TO 10:_     ' also can make following the code more difficult.
  230.      GOSUB Test.I:_    ' Having a lot of little subroutines can muck up
  231.        NEXT                ' your code something terrible. You must use your
  232. END                        ' own judgement as to whether you should kludge up
  233. Test.I:                    ' your code with WHILE-WEND loops and extra varia-
  234. IF I=X THEN PRINT 5 I      ' bles or branch to another subroutine as in this
  235. RETURN                     ' example.
  236.  
  237. 'There is one more way around the problem. Most of the code you write with QB2
  238. 'can also be compiled without change by the IBM BASIC Compiler Version 2. One
  239. 'exception is the IF-THEN-ELSEIF-THEN-ELSE structure. As a result, you may
  240. 'prefer to not use this structure in your programs. If you are not worried
  241. 'about BASCOM2 compatibility, here is how this structure is used:
  242. '                                                                  (PgDn)
  243. 'page 12              (Getting Rid of GOTO's, continued)
  244.  
  245. example6:  INPUT "ENTER A SINGLE-DIGIT NUMBER"; X
  246. IF X=1 THEN
  247.        PRINT "X=1"
  248.   ELSE FOR I=1 TO 10            ' Note the lack of "_" line continuation
  249.      IF I=X THEN PRINT I    ' symbols in this code. That's because the
  250.        NEXT                     ' END IF defines the end of the IF-THEN
  251. END IF                          ' processing, so all the code does not have
  252. END                             ' to be in the same logical line.
  253.  
  254. 'In other examples, we have varied the form of the IF-THEN command to suit
  255. 'our own taste. To use an IF-END_IF block, you must use an exact structure:
  256.  
  257. '  IF xxx THEN            'The THEN must be the last word on the line.
  258. '        <action>
  259. '    ELSEIF yyy THEN      'ELSEIF must be the first word on the line, but
  260. '        <action>         'ELSEIF is optional, as seen in the example above.
  261. '    ELSE <action>
  262. '  END IF
  263.  
  264. '                            end of lesson 10
  265. 'If you were running this program and ended up here at the end of the code
  266. 'because of some error, just press Alt-V, E, Enter to get rid of the error
  267. 'message box. If you think the program is badly messed up, enter Alt-F, L,
  268. 'Enter and reload the program name shown. Otherwise, just press Ctrl-F, enter
  269. 'the page number you were on and use Ctrl-Down to realign the page on the
  270. 'screen.
  271.